/**
 *电表历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const decimal = require("decimal");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils");
const columnNameObj = require("./deviceChartConfig").chart.electric;
const deviceType = require("./deviceChartConfig").type.electric;
module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.active_energy_total AS activeEnergyTotal,
        dd.active_energy_consumption AS activeEnergyConsumption,
        dd.real_active_energy_total AS realActiveEnergyTotal,
        dd.real_active_energy_consumption AS realActiveEnergyConsumption,
        dd.multiplier,
        dd.reverse_active_energy AS reverseActiveEnergy,
        dd.inductive_reactive_power AS inductiveReactivePower,
        dd.capacitive_reactive_power AS capacitiveReactivePower,
        dd.ct_ratio AS ctRatio,
        dd.pt_ratio AS ptRatio,
        phase_a_voltage AS phaseAVoltage,
        phase_b_voltage AS phaseBVoltage,
        phase_c_voltage AS phaseCVoltage,
        line_ab_voltage AS lineAbVoltage,
        line_bc_voltage AS lineBcVoltage,
        line_ac_voltage AS lineAcVoltage,
        phase_a_current AS phaseACurrent,
        phase_b_current AS phaseBCurrent,
        phase_c_current AS phaseCCurrent,
        phase_a_active_power AS phaseAActivePower,
        phase_b_active_power AS phaseBActivePower,
        phase_c_active_power AS phaseCActivePower,
        all_active_power AS allActivePower,
        phase_a_reactive_power AS phaseAReactivePower,
        phase_b_reactive_power AS phaseBReactivePower,
        phase_c_reactive_power AS phaseCReactivePower,
        all_reactive_power AS allReactivePower,
        phase_a_apparent_power AS phaseAApparentPower,
        phase_b_apparent_power AS phaseBApparentPower,
        phase_c_apparent_power AS phaseCApparentPower,
        all_apparent_power AS allApparentPower,
        phase_a_power_factor AS phaseAPowerFactor,
        phase_b_power_factor AS phaseBPowerFactor,
        phase_c_power_factor AS phaseCPowerFactor,
        all_power_factor AS allPowerFactor,
        frequency,
        status,
        rf.range_name AS rangeName
    FROM
    data_electric_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType} 
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let electricDataList = await conn(sql,[params.rangeIds]);
    let electricDataGroup = _.groupBy(electricDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in electricDataGroup){
        let deviceDataList = electricDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            lastReading:deviceDataList[0].activeEnergyTotal,
            realActiveEnergyTotal:deviceDataList[0].realActiveEnergyTotal,
            realActiveEnergyConsumption:deviceDataList[0].realActiveEnergyConsumption,
            multiplier:deviceDataList[0].multiplier,
            reverseActiveEnergy:deviceDataList[0].reverseActiveEnergy,
            inductiveReactivePower:deviceDataList[0].inductiveReactivePower,
            capacitiveReactivePower:deviceDataList[0].capacitiveReactivePower,
            ctRatio:deviceDataList[0].ctRatio,
            ptRatio:deviceDataList[0].ptRatio,
            intervalEnergyConsumption:0,
            phaseAVoltage:deviceDataList[0].phaseAVoltage,
            phaseBVoltage:deviceDataList[0].phaseBVoltage,
            phaseCVoltage:deviceDataList[0].phaseCVoltage,
            lineAbVoltage:deviceDataList[0].lineAbVoltage,
            lineBcVoltage:deviceDataList[0].lineBcVoltage,
            lineAcVoltage:deviceDataList[0].lineAcVoltage,
            phaseACurrent:deviceDataList[0].phaseACurrent,
            phaseBCurrent:deviceDataList[0].phaseBCurrent,
            phaseCCurrent:deviceDataList[0].phaseCCurrent,
            phaseAActivePower:deviceDataList[0].phaseAActivePower,
            phaseBActivePower:deviceDataList[0].phaseBActivePower,
            phaseCActivePower:deviceDataList[0].phaseCActivePower,
            allActivePower:deviceDataList[0].allActivePower,
            phaseAReactivePower:deviceDataList[0].phaseAReactivePower,
            phaseBReactivePower:deviceDataList[0].phaseBReactivePower,
            phaseCReactivePower:deviceDataList[0].phaseCReactivePower,
            allReactivePower:deviceDataList[0].allReactivePower,
            phaseAApparentPower:deviceDataList[0].phaseAApparentPower,
            phaseBApparentPower:deviceDataList[0].phaseBApparentPower,
            phaseCApparentPower:deviceDataList[0].phaseCApparentPower,
            allApparentPower:deviceDataList[0].allApparentPower,
            phaseAPowerFactor:deviceDataList[0].phaseAPowerFactor,
            phaseBPowerFactor:deviceDataList[0].phaseBPowerFactor,
            phaseCPowerFactor:deviceDataList[0].phaseCPowerFactor,
            allPowerFactor:deviceDataList[0].allPowerFactor,
            frequency:deviceDataList[0].frequency,
            status:deviceDataList[0].status,
            dataList:[],
        }
        deviceObj.intervalEnergyConsumption = _.reduce(deviceDataList, function(memo, num){
            return decimal.add(memo,num.activeEnergyConsumption).internal;
        },0)
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    activeEnergyTotal:timeGroupList[0].activeEnergyTotal,
                    activeEnergyConsumption:0,
                    realActiveEnergyTotal:timeGroupList[0].realActiveEnergyTotal,
                    realActiveEnergyConsumption:timeGroupList[0].realActiveEnergyConsumption,
                    multiplier:timeGroupList[0].multiplier,
                    reverseActiveEnergy:timeGroupList[0].reverseActiveEnergy,
                    inductiveReactivePower:timeGroupList[0].inductiveReactivePower,
                    capacitiveReactivePower:timeGroupList[0].capacitiveReactivePower,
                    ctRatio:timeGroupList[0].ctRatio,
                    ptRatio:timeGroupList[0].ptRatio,
                    phaseAVoltage:timeGroupList[0].phaseAVoltage,
                    phaseBVoltage:timeGroupList[0].phaseBVoltage,
                    phaseCVoltage:timeGroupList[0].phaseCVoltage,
                    lineAbVoltage:timeGroupList[0].lineAbVoltage,
                    lineBcVoltage:timeGroupList[0].lineBcVoltage,
                    lineAcVoltage:timeGroupList[0].lineAcVoltage,
                    phaseACurrent:timeGroupList[0].phaseACurrent,
                    phaseBCurrent:timeGroupList[0].phaseBCurrent,
                    phaseCCurrent:timeGroupList[0].phaseCCurrent,
                    phaseAActivePower:timeGroupList[0].phaseAActivePower,
                    phaseBActivePower:timeGroupList[0].phaseBActivePower,
                    phaseCActivePower:timeGroupList[0].phaseCActivePower,
                    allActivePower:timeGroupList[0].allActivePower,
                    phaseAReactivePower:timeGroupList[0].phaseAReactivePower,
                    phaseBReactivePower:timeGroupList[0].phaseBReactivePower,
                    phaseCReactivePower:timeGroupList[0].phaseCReactivePower,
                    allReactivePower:timeGroupList[0].allReactivePower,
                    phaseAApparentPower:timeGroupList[0].phaseAApparentPower,
                    phaseBApparentPower:timeGroupList[0].phaseBApparentPower,
                    phaseCApparentPower:timeGroupList[0].phaseCApparentPower,
                    allApparentPower:timeGroupList[0].allApparentPower,
                    phaseAPowerFactor:timeGroupList[0].phaseAPowerFactor,
                    phaseBPowerFactor:timeGroupList[0].phaseBPowerFactor,
                    phaseCPowerFactor:timeGroupList[0].phaseCPowerFactor,
                    allPowerFactor:timeGroupList[0].allPowerFactor,
                    frequency:timeGroupList[0].frequency,
                    status:timeGroupList[0].status,
                }
                deviceDataObj.activeEnergyConsumption = _.reduce(timeGroupList, function(memo, num){ return decimal.add(memo,num.activeEnergyConsumption).internal; },0)
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);

        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
    }catch (e){
        return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
    }
};